﻿using UnityEngine;
using System.Collections;

public class TicTacToe : MonoBehaviour
{
    private int turn = 1;   // 记录当前回合的操作者,1代表Player1，0代表Player2
    private int[,] state = new int[3, 3];   // 九宫格状态
    private int result = 0; // 0表示游戏进行中，1表示Player1获胜，2表示Player2获胜，3表示平局

    // 界面背景及棋子的插图
    public Texture2D img;
    public Texture2D img1;
    public Texture2D img2;

    // 重置游戏，将游戏状态恢复为初始状态
    void Reset()
    {
        turn = 1;
        result = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                state[i, j] = 0;
            }
        }
    }

    // 开始游戏
    void Start()
    {
        Reset();
    }

    // 判断游戏结束的条件
    int check()
    {
        // 判断横向连线
        for (int i = 0; i < 3; i++)
        {
            if (state[i,0] != 0 && state[i,0] == state[i,1] && state[i,1] == state[i,2])
            {
                return state[i, 0];
            }
        }
        // 判断纵向连线
        for (int j = 0; j < 3; j++)
        {
            if (state[0,j] != 0 && state[0,j] == state[1,j] && state[1,j] == state[2,j])
            {
                return state[0, j];
            }
        }
        // 判断斜向连线
        if (state[1,1]!=0 && 
            (state[0,0] == state[1,1] && state[1,1] == state[2,2] ||
            state[0,2] == state[1,1] && state[1,1] == state[2,0]))
        {
            return state[1, 1];
        }
        // 判断平局
        int count = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (state[i,j] != 0)
                {
                    count++;
                }
            }
        }
        if (count == 9)
        {
            return 3;
        }
        // 如果以上情况都不出现，则返回0默认对局进行中
        return 0;
    }

    void OnGUI()
    {
        // TicTacToe font style
        GUIStyle fontStyle1 = new GUIStyle();
        fontStyle1.normal.background = null;
        fontStyle1.normal.textColor = new Color(1, 0, 1);
        fontStyle1.fontSize = 80;
        // Result font style
        GUIStyle fontStyle2 = new GUIStyle();
        fontStyle2.normal.background = null;
        fontStyle2.normal.textColor = new Color(51, 153, 255);
        fontStyle2.fontSize = 50;
        // Player font style
        GUIStyle fontStyle3 = new GUIStyle();
        fontStyle3.normal.background = null;
        fontStyle3.normal.textColor = new Color(102, 153, 255);
        fontStyle3.fontSize = 30;
        // bullet point font style
        GUIStyle fontStyle4 = new GUIStyle();
        fontStyle4.normal.background = null;
        fontStyle4.normal.textColor = new Color(102, 153, 255);
        fontStyle4.fontSize = 40;

        // background
        GUI.Label(new Rect(0, 0, 1440, 900), img);
        // TicTacToe label
        GUI.Label(new Rect(Screen.width / 2 - 190, 70, 200, 200), "Tic Tac Toe", fontStyle1);
        // players
        GUI.Label(new Rect(Screen.width - 250, 130, 100, 100), img1);
        GUI.Label(new Rect(Screen.width - 140, 165, 200, 100), "Player1", fontStyle3);
        GUI.Label(new Rect(Screen.width - 250, 250, 100, 100), img2);
        GUI.Label(new Rect(Screen.width - 140, 285, 200, 100), "Player2", fontStyle3);
        // other label
        if (turn == 1)
        {
            GUI.Label(new Rect(Screen.width - 300, 161, 100, 50), "➣", fontStyle4);
        }
        else
        {
            GUI.Label(new Rect(Screen.width - 300, 281, 100, 50), "➣", fontStyle4);
        }

        // reset button
        if (GUI.Button(new Rect(Screen.width - 250, 380, 160, 80), "RESET"))
        {
            Reset();
        }

        // 九宫格
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (state[i,j] == 1)    // Player1的棋子
                {
                    GUI.Button(new Rect(Screen.width / 2 - 225 + i * 150, Screen.height / 2 - 225 + j * 150, 150, 150), img1);
                }
                if (state[i, j] == 2)   // Player2的棋子
                {
                    GUI.Button(new Rect(Screen.width / 2 - 225 + i * 150, Screen.height / 2 - 225 + j * 150, 150, 150), img2);
                }
                if (GUI.Button(new Rect(Screen.width / 2 - 225 + i * 150, Screen.height / 2 - 225 + j * 150, 150, 150), ""))   // 空白格
                {
                    if (result == 0)    // 当对局进行中触发按钮
                    {
                        if (turn == 1)  // Player1的回合
                            state[i, j] = 1;
                        else            // Player2的回合
                            state[i, j] = 2;
                        turn = 1 - turn;    // 触发按钮后回合翻转
                    }
                }
            }
        }

        // 对局结束，展示结果
        result = check();
        if (result == 1)
        {
            GUI.Label(new Rect(Screen.width - 350, 600, 200, 100), "Player1 wins!", fontStyle2);
        }
        else if (result == 2)
        {
            GUI.Label(new Rect(Screen.width - 350, 600, 200, 100), "Player2 wins!", fontStyle2);
        }
        else if (result == 3)
        {
            GUI.Label(new Rect(Screen.width - 350, 600, 200, 100), "Tied!!!", fontStyle2);
        }
        else
        {
            if (turn == 1)
            {
                GUI.Label(new Rect(Screen.width - 350, 600, 200, 100), "Player1's turn...", fontStyle2);
            }
            else
            {
                GUI.Label(new Rect(Screen.width - 350, 600, 200, 100), "Player2's turn...", fontStyle2);
            }
        }


    }
}